package com.wj.dubbo.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wj.console.bean.welfareshop.result.ProductDetail;
import com.wj.console.rabbit.message.CrawlerSocialFundMessage;
import com.wj.dubbo.service.config.RabbitMQConfig;
import com.wj.dubbo.service.task.CrawlerTask;
import com.wj.dubbo.service.task.ITaskService;
import com.wj.dubbo.service.welfareshop.service.impl.WelfareShopBasicMsgService;

@Service
public class RabbitMessageListener {
	private static final Logger log = LoggerFactory.getLogger(RabbitMessageListener.class);
	
	@Qualifier("socialTaskService")
	@Autowired
	private ITaskService socialTaskService;
	
	@Qualifier("fundTaskService")
	@Autowired
	private ITaskService fundTaskService;
	
	@Autowired
	private WelfareShopBasicMsgService welfareShopBasicMsgService;
	
    @RabbitListener(queues = RabbitMQConfig.QUEUE_CRAWLER_FUND_NAME)
    public void receiveFundMessage(final GenericMessage<byte[]> message) throws UnsupportedEncodingException{
    	String msg = new String(message.getPayload(),"UTF-8");
    	log.info("接收到公积金爬取任务消息:{}", msg);
    	CrawlerSocialFundMessage crawlerSocialFundMessage = JSON.parseObject(msg, CrawlerSocialFundMessage.class);
    	CrawlerTask task = new CrawlerTask(fundTaskService, crawlerSocialFundMessage);
    	fundTaskService.submitTask(task);
    }

    @RabbitListener(queues = RabbitMQConfig.QUEUE_CRAWLER_SOCIAL_NAME)
    public void receiveSocialMessage(final GenericMessage<byte[]> message) throws UnsupportedEncodingException {
    	String msg = new String(message.getPayload(),"UTF-8");
    	log.info("接收到社保爬取任务消息:{}", msg);
    	CrawlerSocialFundMessage crawlerSocialFundMessage = JSON.parseObject(msg, CrawlerSocialFundMessage.class);
    	CrawlerTask task = new CrawlerTask(socialTaskService, crawlerSocialFundMessage);
    	socialTaskService.submitTask(task);
    }
    
    @RabbitListener(queues = RabbitMQConfig.QUEUE_WELFARESHOP_BATCH_NAME)
    public void receiveBasicMsgBatchMessage(final GenericMessage<byte[]> message) {
    	String msg = new String(message.getPayload());
    	log.info("接收到福利商城跑批任务消息:{}", msg);
    	switch (msg) {
    	//地址
		case "address":	
			welfareShopBasicMsgService.putAllAddress();
			break;
		//商品信息
		case "product":
			welfareShopBasicMsgService.putAllProduct();
			break;
		//商品分类信息
		case "productCate":
			welfareShopBasicMsgService.putAllCate();
			break;
    	//订单、售后单状态
		case "orderStatus":
			welfareShopBasicMsgService.orderStatusBatch();
			break;
		default:
			break;
		}
    }
    
    @RabbitListener(queues = RabbitMQConfig.QUEUE_WELFARESHOP_UPDATE_PRODUCT_NAME)
    public void receiveUpdateProductMessage(final GenericMessage<byte[]> message) throws UnsupportedEncodingException {
    	String msg = new String(message.getPayload(),"UTF-8");
    	log.info("接收到福利商城更新商品信息任务消息:{}", msg);
    	List<ProductDetail> productDetails = JSON.parseArray(msg,ProductDetail.class);
    	if(productDetails!=null&&productDetails.size()!=0){
    		welfareShopBasicMsgService.updateProduct(productDetails);
    	}
    }
}
